const Queue = require('./queue')

function QueueStack() {
  const queue1 = new Queue()
  const queue2 = new Queue()
  let dataQueue = null
  let emptyQueue = null

  function initQueue() {
    if ((queue1.isEmpty() && queue2.isEmpty()) || queue2.isEmpty()) {
      dataQueue = queue1
      emptyQueue = queue2
    } else {
      dataQueue = queue2
      emptyQueue = queue1
    }
  }

  this.push = function (item) {
    initQueue()
    dataQueue.enqueue(item)
  }

  this.pop = function () {
    initQueue()
    while (dataQueue.size() > 1) {
      emptyQueue.enqueue(dataQueue.dequeue())
    }
    return dataQueue.dequeue()
  }

  this.top = function () {
    initQueue()
    return dataQueue.tail()
  }
}

const qs = new QueueStack()
qs.push(1)
qs.push(2)
qs.push(3)
console.log(qs.top())
qs.pop()
console.log(qs.top())
